﻿Option Infer On
Option Explicit On
Option Strict On

Imports System
Imports System.Xml
Imports System.Xml.Linq

Public Module ProjectExtensions
   <Runtime.CompilerServices.Extension()> _
   Friend Function GetDeclaration(ByVal lines As IEnumerable(Of String), ByVal position As Int32) As String
      Dim ret = lines(position)
      For i = position - 1 To 0 Step -1
         If Not lines(i).ContentPortion.TrimEnd.EndsWith("_") Then
            Exit For
         End If
         ret = lines(i) & ret
      Next
      Return ret
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function Indexes( _
            ByVal lines As IEnumerable(Of String), _
            ByVal whereClause As Func(Of String, Boolean)) _
            As IEnumerable(Of Int32)
      Dim ret As New List(Of Int32)
      For i = 0 To lines.Count - 1
         If whereClause(lines(i)) Then
            ret.Add(i)
         End If
      Next
      Return ret
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function ContentPortion(ByVal line As String) As String
      If line.Contains("'") Then
         Return line.SubstringBefore("'")
      End If
      Return line
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function CommentPortion(ByVal line As String) As String
      If line.Contains("'") Then
         Return line.SubstringAfter("'")
      End If
      Return String.Empty
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function ContainsWord(ByVal line As String, ByVal word As String) As Boolean
      Dim position = line.IndexOf(word)
      If position = -1 Then
         Return False
      End If
      Dim startOK As Boolean = False
      Dim endOK As Boolean = False
      If position = 0 Then
         startOK = True
      End If
      If position + word.Length >= line.Length Then
         endOK = True
      End If
      If Not startOK Then
         Dim c = line(position - 1)
         If Char.IsWhiteSpace(c) Or Char.IsPunctuation(c) Or Char.IsSymbol(c) Then
            startOK = True
         End If
      End If
      If Not endOK Then
         Dim c = line(position + word.Length)
         If Char.IsWhiteSpace(c) Or Char.IsPunctuation(c) Or Char.IsSymbol(c) Then
            endOK = True
         End If
      End If
      Return startOK And endOK
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function SubstringBefore( _
         ByVal searchIn As String, _
         ByVal searchFor As String) _
         As String
      Dim pos = searchIn.IndexOf(searchFor)
      If pos = -1 Then
         Return String.Empty
      End If
      Return searchIn.Substring(0, pos)
   End Function

   <Runtime.CompilerServices.Extension()> _
   Friend Function SubstringAfter( _
            ByVal searchIn As String, _
            ByVal searchFor As String) _
            As String
      Dim pos = searchIn.IndexOf(searchFor)
      If pos = -1 Then
         Return String.Empty
      End If
      Return searchIn.Substring(pos + searchFor.Length)
   End Function


End Module
